<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 800px;
            height: 500px;
            background-color: pink;
            margin: 400px auto;
        }

        a {
            text-decoration: none;
            color: #fff;
            cursor: pointer;
        }

        .back {
            position: fixed;
            right: 20px;
            bottom: 20px;
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: purple;
            color: #fff;
            text-align: center;
            display: none;
        }
    </style>
    <script src="jQuery.min.js"></script>
</head>

<body>
    <div class="box">

    </div>
    <div class="back">
        <a href="javascript:;">返回顶部</a>
    </div>
    <script>
        $(function () {
        
            //被卷去的头部 scrollTop() / 被卷去的左侧 scroll Left()
            var boxTop = $('.box').offset().top;
            $(window).scroll(function () {
                // console.log(11);
                console.log($(document).scrollTop());
                if ($(document).scrollTop() >= boxTop) {
                    $('.back').fadeIn();
                } else {
                    $(".back").fadeOut();
                }
            });
            //返回顶部
            $(".back").click(function () {
                // $(document).scrollTop(0);
                $('body,html').stop().animate({
                    scrollTop: 0
                });
                //不能是文档 而是HTML 和body 元素做动画
            })
        })
    </script>
</body>

</html>